package com.hub.service.asset.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hub.service.impl.BaseServiceImpl;
import com.hub.dto.asset.AssetInfoFileDto;
import com.hub.entity.asset.AssetInfoFileEntity;
import com.hub.frame.consts.ConstantUtil;
import com.hub.utils.ApiResultObject;
import com.hub.mapper.asset.AssetInfoFilesMapper;
import com.hub.model.asset.AssetInfoFileModel;
import com.hub.service.asset.IAssetInfoFilesService;
import com.hub.utils.enums.FileTypeEnum;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hub
 * @since 2020-05-21
 */
@Service
public class AssetInfoFilesServiceImpl extends BaseServiceImpl<AssetInfoFilesMapper, AssetInfoFileEntity> implements IAssetInfoFilesService {

    @Autowired
    private Mapper mapper;

    /**
     * 根据资产ID，获取该资产所有的附件信息
     * @param assetId
     * @return
     */
    public List<AssetInfoFileModel> loadAssetFilesByAssetId(long assetId) {

        QueryWrapper<AssetInfoFileEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("asset_id",assetId);
        wrapper.eq("active_flg", ConstantUtil.YES);

        List<AssetInfoFileEntity> list = baseMapper.selectList(wrapper);
        List<AssetInfoFileModel> modelList = new ArrayList<>();

        for (AssetInfoFileEntity entity:list) {
            modelList.add(mapper.map(entity,AssetInfoFileModel.class));
        }

        return modelList;
    }

    /**
     * 根据资产ID及文件类型，获取该资产所有的附件信息
     * @param assetId
     * @return
     */
    public List<AssetInfoFileModel> loadAssetFilesByAssetIdAndType(long assetId, FileTypeEnum type) {

        QueryWrapper<AssetInfoFileEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("asset_id",assetId);
        wrapper.eq("file_type",type.getCode());
        wrapper.eq("active_flg", ConstantUtil.YES);

        List<AssetInfoFileEntity> list = baseMapper.selectList(wrapper);
        List<AssetInfoFileModel> modelList = new ArrayList<>();

        for (AssetInfoFileEntity entity:list) {
            modelList.add(mapper.map(entity,AssetInfoFileModel.class));
        }

        return modelList;
    }

    /**
     * 新增资产基础信息文件
     * @param request
     * @return
     */
    public ApiResultObject addAssetInfoFile(AssetInfoFileDto request) {

        //保存数据库
        AssetInfoFileEntity entity = mapper.map(request, AssetInfoFileEntity.class);

        if(this.save(entity)){
            return ApiResultObject.success("新增资产基础信息文件成功!");
        }
        return ApiResultObject.failed("资产信息文件保存数据库失败!");
    }

    /**
     * 批量新增资产基础信息文件
     * @param request
     * @return
     */
    @Transactional
    public ApiResultObject addAssetInfoFiles(List<AssetInfoFileDto> request) {

        for (AssetInfoFileDto model:request) {
            //保存数据库
            AssetInfoFileEntity entity = mapper.map(model, AssetInfoFileEntity.class);
            if(!this.save(entity)){
                return ApiResultObject.failed("资产信息文件保存数据库失败!");
            }
        }
        return ApiResultObject.success("新增资产基础信息文件成功!");
    }

    /**
     * 删除资产基础信息文件-逻辑删除
     * @param id
     * @return
     */
    public ApiResultObject removeAssetInfoFile(Long id) {

        if(id< 1)
            return ApiResultObject.failed("参数异常: 资产信息附件ID异常!");

        if(this.removeById(id)){
            return ApiResultObject.success("资产基础信息附件删除成功!");
        }
        return ApiResultObject.failed("资产基础信息附件异常，删除失败!");
    }

}
